String.prototype.format = function (args) {
    if (arguments.length > 0) {
        var result = this;
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                var reg = new RegExp("({" + key + "})", "g");
                result = result.replace(reg, args[key]);
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] == undefined) {
                    return "";
                }
                else {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
        return result;
    }
    else {
        return this;
    }
};

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _mima: null,
        _mimaIndex: 0,
        _agreement: true
    },

    // use this for initialization
    onLoad: function () {
        if (!cc.sys.isNative && cc.sys.isMobile) {
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }

        if (!cc.vv) {
            cc.director.loadScene("loading");
            return;
        }
        cc.vv.http.url = cc.vv.http.master_url;
        // cc.vv.net.addHandler('push_need_create_role', function () {
        //     console.log("onLoad:push_need_create_role");
        //     cc.director.loadScene("createrole");
        // });

        // cc.vv.audioMgr.playBGM("bgMain.mp3");

        this._mima = ["A", "A", "B", "B", "A", "B", "A", "B", "A", "A", "A", "B", "B", "B"];

        if (!cc.sys.isNative || cc.sys.os == cc.sys.OS_WINDOWS) {
            cc.find("Canvas/btn_yk").active = true;
        }
    },

    start: function () {
        var account = cc.sys.localStorage.getItem("wx_account");
        var sign = cc.sys.localStorage.getItem("wx_sign");
        if (account != null && sign != null) {
            var ret = {
                errcode: 0,
                account: account,
                sign: sign
            }
            cc.vv.userMgr.onAuth(ret);
        }
    },

    onBtnQuickStartClicked: function () {
        if (this._agreement) {
            cc.vv.userMgr.guestAuth();
        } else {
            // console.log('必须同意用户使用协议才可以登录游戏')
            cc.vv.alert.show("必须同意用户使用协议才可以登录游戏");
        }
    },

    onBtnWeichatClicked: function () {
        if (this._agreement) {
            cc.vv.anysdkMgr.login();
        } else {
            // console.log('必须同意用户使用协议才可以登录游戏')
            cc.vv.alert.show("必须同意用户使用协议才可以登录游戏");
        }
    },

    onBtnMIMAClicked: function (event) {
        if (this._mima[this._mimaIndex] == event.target.name) {
            this._mimaIndex++;
            if (this._mimaIndex == this._mima.length) {
                cc.find("Canvas/btn_yk").active = true;
            }
        }
        else {
            this._mimaIndex = 0;
        }
    },

    onBtnAgreementColse: function () {
        var aaa = cc.find('Canvas/Agreement/btn_checkbox/check_mark')
        aaa.active = !aaa.active;
        this._agreement = aaa.active;
        console.log(this._agreement)

    },

    onBtnAgreementClicked: function () {
        var Agreement_alert = cc.find("Canvas/Agreement_alert");
        Agreement_alert.active = true;
    },

    onBtnCloseAgreementClicked: function () {
        var Agreement_alert = cc.find("Canvas/Agreement_alert");
        Agreement_alert.active = false;
    },
});
